package com.jbm.enterapplication.userlogin.view

import android.content.Intent
import android.support.design.widget.TextInputLayout
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import com.jbm.enterapplication.R
import com.jbm.enterapplication.activity.MainActivity
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.userlogin.presenter.User_Present_Login
import com.jbm.enterapplication.utlisclass.Constant.*
import com.jbm.enterapplication.view.DialogLoading
import com.jbm.jbmsupplier.activity.register.view.Register_Construction
import com.tencent.mm.sdk.modelmsg.SendAuth
import com.tencent.mm.sdk.openapi.WXAPIFactory
import kotlinx.android.synthetic.main.activity_user_login.*



class UserLogin : MainActivity(),User_View_Login_In ,View.OnClickListener {
    override fun finishs() {
        finish()
    }

     var  dialogLoading: DialogLoading?=null

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.bu_login -> {
                userpresentlogin?.getUserLogin()
            }
            R.id.text_register -> {
                startActivity(Intent(this@UserLogin, Register_Construction::class.java))
            }
            R.id.text_wjmm -> {//微信登录
                if (!wxApi!!.isWXAppInstalled()) {
                    Toast.makeText(this, "请先安装微信应用", Toast.LENGTH_SHORT).show()

                } else if (!wxApi!!.isWXAppSupportAPI()) {
                    Toast.makeText(this, "请先更新微信应用", Toast.LENGTH_SHORT).show()

                } else {
                    // 将该app注册到微信
                    isWXLogin = true
                    val req = SendAuth.Req()
                    req.scope = "snsapi_userinfo"
                    req.state = "wechat_sdk_demo"
                    wxApi!!.sendReq(req)
                    dialogLoading = DialogLoading(this, "登录中.....", true)
                    dialogLoading?.show()
                }

            }


        }
    }

    override fun onResume() {
        super.onResume()
        if (isWXLogin) {
            if (dialogLoading!=null){
                dialogLoading?.dismiss()
            }

            userpresentlogin?.getWXLogin()
        }
    }

    override fun getUserNameerr(): TextInputLayout {
        return ednames
    }

    override fun getUserPwderr(): TextInputLayout {
        return edpwds
    }

    override fun getUserName(): String {
        return edname.editableText.toString()
    }

    override fun getUserPwd(): String {
        return edpwd.editableText.toString()
    }

    override fun getView(): Int {
        return R.layout.activity_user_login
    }


    var userpresentlogin: User_Present_Login? = null
    var shanderprefter: SharedPreActivity? = null


    override fun getinit() {
         userpresentlogin= User_Present_Login(this,this)
        wxApi = WXAPIFactory.createWXAPI(this, WX_APP_ID, true)
        wxApi !!.registerApp(WX_APP_ID)

        bu_login.setOnClickListener(this)
        text_register.setOnClickListener(this)
        edname.addTextChangedListener( MyWatcher(edname))
        edpwd.addTextChangedListener( MyWatcher(edpwd))
        shanderprefter= SharedPreActivity(this)
        text_wjmm.setOnClickListener(this)
        if (!shanderprefter?.getIsUser().equals("")){
            var namepwd = shanderprefter?.getIsUser()
            var name = namepwd!!.substring(0, namepwd.indexOf(","))
            var pwd = namepwd!!.substring(namepwd.indexOf(",") + 1, namepwd!!.length)
            edname.setText(name)
            edpwd.setText(pwd)
        }


    }

    inner  internal class MyWatcher : TextWatcher {
        var view: View?=null
        constructor(view: View?) {
            this.view = view
        }
        override fun afterTextChanged(s: Editable?) {
            when(view?.id){
                R.id.edname ->{
                    if (s!!.length>=11){
                        ednames.isErrorEnabled=false
                    }else if (!s.toString().equals(""))
                        ednames.isErrorEnabled=false
                }
                R.id.edpwd ->{
                    if (!s.toString().equals(""))
                        edpwds.isErrorEnabled=false
                }

            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }



}
